﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;

namespace Common
{
    public class SysHelper
    {
        public static UserPrincipal CurrentPrincipal
        {
            get
            {
                return HttpContext.Current.User as UserPrincipal;
            }
        }
    }

    public class UserPrincipal : ClientUserData,IPrincipal
    {
        public IIdentity Identity { get; private set; }
        public string[] Roles { get; set; }

        public UserPrincipal(ClientUserData clientUserData)
        {
            //this.Identity = new GenericIdentity(string.Format("{0}", clientUserData.id));

            this.Identity = new GenericIdentity(string.Format("{0}", clientUserData.UserNo));
            this.UserNo = clientUserData.UserNo;
            this.UserName = clientUserData.UserName;
            this.UserDept = clientUserData.UserDept;

            this.UserRole = clientUserData.UserRole;
            this.UserView = clientUserData.UserView;
        }


        public bool IsInRole(string role)
        {
            if (Roles.Any(r => r == role))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public class ClientUserData
    {
        public string UserNo { get; set; }

        public string UserName { get; set; }
        public string UserDept { get; set; }

        public string UserRole { get; set; }

        public string UserView { get; set; }

    }
}